package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.User;
import service.UserManager;
import service.impl.UserManagerImpl;

@WebServlet("/AjaxServlet")
public class AjaxServlet extends HttpServlet {
	private UserManager userManager;
	public AjaxServlet() {
		super();
	}

	public void destroy() {
		super.destroy();

	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("#####");
		response.setContentType("text/xml;charset=UTF-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String result = "";
		StringBuffer sb = new StringBuffer();
//		if ("admin".equals(username) && "123456".equals(password)) {
//			sb.append("<text>登陆成功</text>");
//			//result = "<text>登陆成功</text>";
//		} else {
//			sb.append("<text>登陆失败</text>");
//		}
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		if(userManager.exist(user)){
			sb.append("{\"result\":\"true\",\"errCode\":\"1001用户存在\"}");
			//sb.append("[{\"result\":\"true\",\"errCode\":\"1001用户存在\"},{\"result\":\"true\",\"errCode\":\"1001用户存在\"}]");
		}else{
			sb.append("{\"result\":\"false\",\"errCode\":\"1002用户不存在\"}");
			//sb.append("[{\"result\":\"flase\",\"errCode\":\"1002用户不存在\"},{\"result\":\"flase\",\"errCode\":\"1002用户存在\"}]");
		}
		response.getWriter().print(sb.toString());
		response.getWriter().close();
		// PrintWriter out = response.getWriter();
		// String name = request.getParameter("name");
		// String pass = request.getParameter("pass");
		//
		//// 这里为了方便就硬性的给了两个值，当然你可以从数据库中得到
		// if(name.equals("luofang")&&pass.equals("123")){
		// out.print("suc");//用户名密码正确就返回字符suc
		// } else {
		// out.print("fail");//失败就返回字符fail
		// }
		// out.flush();
		// out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("#####");
		response.setContentType("text/xml;charset=UTF-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String result = "";
		StringBuffer sb = new StringBuffer();
//		if ("admin".equals(username) && "123456".equals(password)) {
//			sb.append("<text>登陆成功</text>");
//			//result = "<text>登陆成功</text>";
//		} else {
//			sb.append("<text>登陆失败</text>");
//		}
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		if(userManager.exist(user)){
			sb.append("<text>登陆成功</text>");
		}else{
			sb.append("<text>登陆失败</text>");
		}
		response.getWriter().print(sb.toString());
		response.getWriter().close();
		// PrintWriter out = response.getWriter();
		// String name = request.getParameter("name");
		// String pass = request.getParameter("pass");
		//
		//// 这里为了方便就硬性的给了两个值，当然你可以从数据库中得到
		// if(name.equals("luofang")&&pass.equals("123")){
		// out.print("suc");//用户名密码正确就返回字符suc
		// } else {
		// out.print("fail");//失败就返回字符fail
		// }
		// out.flush();
		// out.close();
	}

	public void init() throws ServletException {
		userManager = new UserManagerImpl();
		// Put your code here
	}

}